﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BenevolentSunData
{
    public class SourceCell : Cell
    {

        public override string Name
        {
            get
            {
                return "Light Source";
            }
        }

        public override string Note
        {
            get
            {
                return "Emits a beam of light";
            }
        }


        #region Graphics
        [ContentSerializerIgnore]
        public Model Model;

        [ContentSerializerIgnore]
        public Material Material = new Material();
        #endregion


        #region State
        Direction beamDirection = Direction.North;
        public Direction BeamDirection
        {
            get { return beamDirection; }
            set
            {
                beamDirection = value;
                switch (beamDirection)
                {
                    case Direction.North:
                        Rotation = (float)Math.PI;
                        break;
                    case Direction.South:
                        Rotation = 0;
                        break;
                    case Direction.East:
                        Rotation = (float)Math.PI * 0.5f;
                        break;
                    case Direction.West:
                        Rotation = (float)Math.PI * 1.5f;
                        break;
                }
            }
        }

        Color beamColor;
        [ContentSerializer(Optional = true)]
        public Color BeamColor
        {
            get { return beamColor; }
            set { beamColor = value; Material.EmissiveColor = new Color(beamColor.ToVector3() * 0.3f); }
        }
        #endregion


        #region Graphics
        Matrix[] boneTransforms;
        #endregion


        #region Initialization
        public SourceCell()
        {
            BeamDirection = Direction.North;
            BeamColor = Color.White;
        }

        public override void LoadContent(ContentManager Content, Map map)
        {
            Material = map.Skin.GetMaterial("Source");
            Model = Content.Load<Model>("Models/Source");
            boneTransforms = new Matrix[Model.Bones.Count];
            Model.CopyAbsoluteBoneTransformsTo(boneTransforms);
            BeamColor = beamColor;
            base.LoadContent(Content, map);
        }
        #endregion


        #region Beams
        public Direction CreateBeam()
        {
            MergeBeam(new Beam(), beamDirection);
            GetBeam(beamDirection).Color = BeamColor;
            //MergeBeam(GetBeam(beamDirection), DirectionUtils.Invert(beamDirection));

            return beamDirection;
        }

        public override void Clear()
        {
            base.Clear();
        }

        public override List<Direction> AddInput(Beam beam, Direction direction)
        {
            if (direction == beamDirection)
            {
                MergeBeam(beam, beamDirection);
            }
            return base.AddInput(beam, direction);
        }
        #endregion


        #region Drawing
        public override void Draw(Vector3 position)
        {
            foreach (ModelMesh mesh in Model.Meshes)
            {
                Matrix world =
                    boneTransforms[mesh.ParentBone.Index] *
                    Matrix.CreateFromYawPitchRoll(Rotation, 0f, 0f) *
                    Matrix.CreateTranslation(position);
                Material.DrawMesh(mesh, world, Map.PointLights, Map.BeamLights);
            }
        }

        #endregion


        #region ICloneable
        public override object Clone()
        {
            SourceCell cell = new SourceCell();
            cell.Coords = new Point(Coords.X, Coords.Y);
            cell.BeamDirection = BeamDirection;
            cell.BeamColor = BeamColor;
            cell.Model = Model;
            cell.Material = Material.Clone();
            cell.boneTransforms = boneTransforms;
            cell.floorBoneTransforms = floorBoneTransforms;
            cell.floorModel = floorModel;
            cell.floorMaterial = floorMaterial.Clone();
            return cell;
        }
        #endregion

    }
}
